﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Mvc;
using Model;
using BLL;
using System.Web.UI.DataVisualization.Charting;
using System.IO;
using System.Text;

namespace OA.Controllers
{
    public class AgeanalyseChartController : Controller
    {
        //
        // GET: /AgeanalyseChart/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AgeanalyseChart()
        {
            consumeSolutionManager CSManager = new consumeSolutionManager();
            List<consumeSolution> CSList = CSManager.GetAll().ToList();
            List<SelectListItem> listLevel = new List<SelectListItem>();
            for (int i = 0; i < CSList.Count(); i++)
            {
                listLevel.Add(new SelectListItem { Text = CSList[i].solutionName, Value = CSList[i].id.ToString() });
            }
            ViewData["memberLevel"] = listLevel;
            return View();
        }

        public FileResult Pie()
        {
            memberInfo MI = new memberInfo();
            memberInfoManager MIManager = new memberInfoManager();
            StringBuilder sb = new StringBuilder();
            string sex = System.Web.HttpContext.Current.Request.QueryString["sex"];
            string memberLevel = System.Web.HttpContext.Current.Request.QueryString["memberLevel"];
            string consume = System.Web.HttpContext.Current.Request.QueryString["consume"];
            string inputNum = System.Web.HttpContext.Current.Request.QueryString["inputNum"];
            DataTable ds1 = null;
            
            List<string> nianling = new List<string>();
            List<string> renshu = new List<string>();
            int num = 0, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0;
            if (!string.IsNullOrEmpty(sex) && !string.IsNullOrEmpty(memberLevel))
            {
                consumeSolutionManager CSManager = new consumeSolutionManager();
                List<consumeSolution> CSList = CSManager.GetAll().ToList();
                int allNUm = 0;
                for (int i = 0; i < CSList.Count(); i++)
                {
                    allNUm += MIManager.getNumByCardType(CSList[i].id);

                }
                ds1 = MIManager.getByAge(sex, memberLevel, consume, inputNum);
                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 0 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 16)
                    {
                        num += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num != 0)
                {
                    string aa = (decimal.Parse(num.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa);
                    nianling.Add("0-16" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 17 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 22)
                    {
                        num1 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num1 != 0)
                {
                    string aa1 = (decimal.Parse(num1.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa1);
                    nianling.Add("17-22" + "岁  比例：" + decimal.Parse(aa1).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 23 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 30)
                    {
                        num2 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num2 != 0)
                {
                    string aa2 = (decimal.Parse(num2.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa2);
                    nianling.Add("23-30" + "岁  比例：" + decimal.Parse(aa2).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 31 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 40)
                    {
                        num3 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num3 != 0)
                {
                    string aa3 = (decimal.Parse(num3.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa3);
                    nianling.Add("31-40" + "岁  比例：" + decimal.Parse(aa3).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 41 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 50)
                    {
                        num4 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num4 != 0)
                {
                    string aa4 = (decimal.Parse(num4.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa4);
                    nianling.Add("41-50" + "岁  比例：" + decimal.Parse(aa4).ToString("F1") + "%");
                }
                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 51)
                    {
                        num5 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num5 != 0)
                {
                    string aa5 = (decimal.Parse(num5.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                    renshu.Add(aa5);
                    nianling.Add("50" + "岁以上  比例：" + decimal.Parse(aa5).ToString("F1") + "%");
                }
                int lessAllNum=allNUm-(num+num1+num2+num3+num4+num5);

                string aa6 = (decimal.Parse(lessAllNum.ToString()) / decimal.Parse(allNUm.ToString()) * decimal.Parse("100")).ToString();
                renshu.Add(aa6);
                nianling.Add("其他" + "比例：" + decimal.Parse(aa6).ToString("F1") + "%");
            }
            else
            {
                decimal zonghe1 = 0;
                ds1 = MIManager.getByAge();
                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    zonghe1 = zonghe1 + decimal.Parse(ds1.Rows[i]["人数"].ToString());
                }
                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 0 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 16)
                    {
                        num += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num != 0)
                {
                    string aa = ((100 / zonghe1) * decimal.Parse(num.ToString())).ToString();
                    renshu.Add(aa);
                    nianling.Add("0-16" + "岁  比例：" + decimal.Parse(aa).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 17 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 22)
                    {
                        num1 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num1 != 0)
                {
                    string aa1 = ((100 / zonghe1) * decimal.Parse(num1.ToString())).ToString();
                    renshu.Add(aa1);
                    nianling.Add("17-22" + "岁  比例：" + decimal.Parse(aa1).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 23 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 30)
                    {
                        num2 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num2 != 0)
                {
                    string aa2 = ((100 / zonghe1) * decimal.Parse(num2.ToString())).ToString();
                    renshu.Add(aa2);
                    nianling.Add("23-30" + "岁  比例：" + decimal.Parse(aa2).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 31 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 40)
                    {
                        num3 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num3 != 0)
                {
                    string aa3 = ((100 / zonghe1) * decimal.Parse(num3.ToString())).ToString();
                    renshu.Add(aa3);
                    nianling.Add("31-40" + "岁  比例：" + decimal.Parse(aa3).ToString("F1") + "%");
                }

                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 41 && int.Parse(ds1.Rows[i]["年龄"].ToString()) <= 50)
                    {
                        num4 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num4 != 0)
                {
                    string aa4 = ((100 / zonghe1) * decimal.Parse(num4.ToString())).ToString();
                    renshu.Add(aa4);
                    nianling.Add("41-50" + "岁  比例：" + decimal.Parse(aa4).ToString("F1") + "%");
                }
                for (int i = 0; i < ds1.Rows.Count; i++)
                {
                    if (int.Parse(ds1.Rows[i]["年龄"].ToString()) >= 51)
                    {
                        num5 += Convert.ToInt32(ds1.Rows[i]["人数"]);

                    }
                }
                if (num5 != 0)
                {
                    string aa5 = ((100 / zonghe1) * decimal.Parse(num5.ToString())).ToString();
                    renshu.Add(aa5);
                    nianling.Add("50" + "岁以上  比例：" + decimal.Parse(aa5).ToString("F1") + "%");
                }
            }
          
           
           Chart chart1 = new Chart();
           chart1.Height = 450;
           chart1.Width = 850;
           chart1.RenderType = System.Web.UI.DataVisualization.Charting.RenderType.ImageTag;
           chart1.Palette = ChartColorPalette.BrightPastel; 
           Title t = new Title("年龄分析饼图", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
           chart1.Titles.Add(t);
           chart1.ChartAreas.Add("view");
           chart1.Series.Add("Series1");
           chart1.ChartAreas["view"].Area3DStyle.Enable3D = true;
           chart1.ChartAreas[0].AxisX.Interval = 1;
           chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
           // CustomerChart.BackColor = System.Drawing.Color.Red;
           chart1.BorderlineWidth = 2;
           chart1.BorderColor = System.Drawing.Color.Black;
           chart1.BorderlineDashStyle = ChartDashStyle.Solid;
           chart1.BorderWidth = 1;
           chart1.Series["Series1"].ChartType = SeriesChartType.Pie;
           chart1.Series["Series1"]["PieLabelStyle"] = "Outside";
           chart1.Series["Series1"]["PieLinelColor"] = "Red";
           chart1.Series[0].Points.DataBindXY(nianling, renshu);
           chart1.DataBind();
           MemoryStream imageStream = new MemoryStream();
           chart1.SaveImage(imageStream, ChartImageFormat.Png);
           imageStream.Position = 0;
           return new FileStreamResult(imageStream, "image/png");
        }
    }
}
